import os
import re
import openpyxl

regex_dict = {
    "os_version": r'\n\s*PRETTY_NAME="(?P<os_version>.*)"',
    "max_core_file_size_soft": r'\n\s*Max core file size\s*(?P<max_core_file_size_soft>[a-z0-9]*)\s*(?P<max_core_file_size_hard>[a-z0-9]*)',
    "max_core_file_size_hard": r'\n\s*Max core file size\s*(?P<max_core_file_size_soft>[a-z0-9]*)\s*(?P<max_core_file_size_hard>[a-z0-9]*)',
    "max_open_file_soft": r'\n\s*Max open files\s*(?P<max_open_file_soft>[a-z0-9]*)\s*(?P<max_open_file_hard>[a-z0-9]*)',
    "max_open_file_hard": r'\n\s*Max open files\s*(?P<max_open_file_soft>[a-z0-9]*)\s*(?P<max_open_file_hard>[a-z0-9]*)',
    "max_process_soft": r'\n\s*Max processes\s*(?P<max_process_soft>[a-z0-9]*)\s*(?P<max_process_hard>[a-z0-9]*)',
    "max_process_hard": r'\n\s*Max processes\s*(?P<max_process_soft>[a-z0-9]*)\s*(?P<max_process_hard>[a-z0-9]*)',
    "ssl": r'\n\s*listen\s+.*(?P<ssl>ssl).*',
    "gzip": r'\n\s*gzip\s+.*(?P<gzip>on).*',
    "openssl": r'\n\s*built with OpenSSL (?P<openssl>[0-9.]*[a-z]) .*',
    "cpu_cores": r'\n\s*CPU\(s\)\:\s+(?P<cpu_cores>\d+)',
    "cpu_usage_idle": r'\n\s*%Cpu\(s\):.*ni, (?P<cpu_usage_idle>[0-9.]*) id,.*',
    "nginx_user": r'\n\s*user\s*(?P<nginx_user>.*);.*',
    "nginx_version": r'\n\s*nginx version: (?P<nginx_version>[a-zA-Z/]*[0-9.]+)',
    "nginx_modules": r'\n\s*configure arguments: --(?P<nginx_modules>.*)',
    "max_open_file": r'\n\s*Max open files \W*(?P<max_open_file>[0-9]+).*',
    "worker_processes": r'\n\s*worker_processes\s*(?P<worker_processes>[0-9]*|auto);.*',
    "worker_connections": r'\n\s*worker_connections\s*(?P<worker_connections>\d+);.*',
    "worker_rlimit_nofile": r'\n\s*worker_rlimit_nofile\s*(?P<worker_rlimit_nofile>\d+);.*',
    "keepalive_timeout": r'\n\s*keepalive_timeout\s*(?P<keepalive_timeout>[0-9s]+);.*',
    "client_max_body_size": r'\n\s*client_max_body_size\s+(?P<client_max_body_size>\d+[a-zA-Z]);.*',
    "server_tokens": r'\n\s*server_tokens\s+(?P<server_tokens>\w+);.*',
    "autoindex": r'\n\s*autoindex\s+(?P<autoindex>\w+);.*',
    "proxy_buffering": r'\n\s*proxy_buffering\s+(?P<proxy_buffering>\w+);.*',
    "proxy_buffer_size": r'\n\s*proxy_buffer_size\s+(?P<proxy_buffer_size>[0-9]+[0-9km ]+);.*',
    "proxy_buffers": r'\n\s*proxy_buffers\s+(?P<proxy_buffers>[0-9]+[0-9km ]+);.*',
    "proxy_busy_buffers_size": r'\n\s*proxy_busy_buffers_size\s+(?P<proxy_busy_buffers_size>[0-9]+[0-9km ]+);.*',
    "memory_size": r'\n\s*Mem:\W+(?P<memory_size>[0-9]+)\W+(?P<memory_used>[0-9]+)\W+(?P<memory_free>[0-9]+)\W+(?P<memory_shared>[0-9]+)\W+(?P<memory_buff_cache>[0-9]+).*',
    "memory_used": r'\n\s*Mem:\W+(?P<memory_size>[0-9]+)\W+(?P<memory_used>[0-9]+)\W+(?P<memory_free>[0-9]+)\W+(?P<memory_shared>[0-9]+)\W+(?P<memory_buff_cache>[0-9]+).*',
    "with_pcre": r'\n\s*.* --with-pcre=(?P<with_pcre>.*?) .*',
    "with_zlib": r'\n\s*.* --with-zlib=(?P<with_zlib>.*?) .*',
    "http_realip_module": r'\n\s*.* --(?P<http_realip_module>with-http_realip_module) .*',
    "http_ssl_module": r'\n\s*.* --(?P<http_ssl_module>with-http_ssl_module) .*',
    "http_v2_module": r'\n\s*.* --(?P<http_v2_module>with-http_v2_module) .*',
    "pcre_version": r'\n\s*pcre版本收集: (?P<pcre_version>[0-9.]*)',
    "pcre_devel_version": r'\n\s*pcre-devel版本收集:pcre-devel-(?P<pcre_devel_version>[0-9.]*)-.*',
    "zlib_devel_version": r'\n\s*zlib-devel版本收集:zlib-devel-(?P<zlib_devel_version>[0-9.]*)-.*',
    "tcp_max_syn_backlog": r'\n\s*net.ipv4.tcp_max_syn_backlog\s*=\s*(?P<tcp_max_syn_backlog>[0-9]*)',
    "tcp_syncookies": r'\n\s*net.ipv4.tcp_syncookies\s*=\s*(?P<tcp_syncookies>[0-9]*)',
    "tcp_synack_retries": r'\n\s*net.ipv4.tcp_synack_retries\s*=\s*(?P<tcp_synack_retries>[0-9]*)',
    "tcp_timestamps": r'\n\s*net.ipv4.tcp_timestamps\s*=\s*(?P<tcp_timestamps>[0-9]*)',
    "tcp_tw_reuse": r'\n\s*net.ipv4.tcp_tw_reuse\s*=\s*(?P<tcp_tw_reuse>[0-9]*)',
    "somaxconn": r'\n\s*net.core.somaxconn\s*=\s*(?P<somaxconn>[0-9]*)',
}

if __name__ == '__main__':

    file = 'nginx_10.20.6.178_20241206120311.log'
    with open(file, 'r', encoding='utf-8', errors='ignore') as file:
        content = file.read()
        data = {}
        for regex in regex_dict.keys():
            match = re.search(regex_dict[regex], content)
            if match:
                value = match.group(regex)
            else:
                value = ""
            data[regex] = value
        print(data)
        print(data['server_tokens'])


